import { ArgumentMetadata, HttpException, HttpStatus, Injectable, PipeTransform } from '@nestjs/common';
import { plainToInstance } from 'class-transformer'
import {validate } from 'class-validator'
@Injectable()
export class LoginPipe implements PipeTransform {
 async transform(value: any, metadata: ArgumentMetadata) {
    // console.log('value=>',value,'metadata=>',metadata);
   const DTO = plainToInstance(metadata.metatype, value)
  //  console.log(DTO);
   const errors = await validate(DTO) //这个promise 返回一个失败信息的数组
   console.log("error",errors);
   if (errors.length) {
      throw new HttpException(errors,HttpStatus.BAD_REQUEST);//参数错误返回400
   }
    return value;
  }
}
